<?php

$json = array('status' => 'error', 'msg' => '', 'data' => null);

$phone = fn_post('phone');
$pwd = fn_post('pwd');

if (!preg_match('/^1\d{10}$/', $phone)) {
	$json['msg'] = '手机格式错误!';
} elseif ($this->website['class']['db']
		->table('user')
		->where('phone=?', $phone)
		->limit(1)
		->count()) {
	$json['msg'] = '手机号码已被注册，忘记密码可以用手机号找回!';
} else {
	$uid = $this->website['class']['db']
		->table('user')
		->field('phone', 'visible', 'authority', 'createtime')
		->value($phone, 1, 0, $_SERVER['REQUEST_TIME'])
		->insert();
	if ($uid) {
		$this->website['class']['db']
			->table('user')
			->field('pwd', 'uname')
			->value(password_hash($uid . $pwd, PASSWORD_DEFAULT), '用户' . $uid)
			->where('id=?', $uid)
			->update();
		$json['status'] = 'ok';
		$json['msg'] = '注册成功';

		$user = $this->website['class']['db']
			->table('user')
			->where('id=?', $uid)
			->one();
		$user['token'] = password_hash($user['id'] . $this->website['safe']['key'] . date('Y'), PASSWORD_DEFAULT);
		unset($user['pwd']);
		$json['data'] = $user;
	} else {
		$json['msg'] = '账户生成失败';
	}
}

$this->json_encode_encrypt($json);
